'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { INTEGER, DATE, STRING } = app.Sequelize;
  const Vote = app.model.define(
    'vote',
    {
      id: {type: INTEGER, primaryKey: true, autoIncrement: true},
      user_id: INTEGER(11),
      group_id:INTEGER(11),
      password: STRING(255),
      create_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      }
    },
    {
      tableName: 'vote',
      timestamps: true, // 启用时间戳(createdAt, updatedAt)
      updatedAt: 'updated_time', // 不想要 updatedAt
      createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
    }
  );
  Vote.associate = function() {
    app.model.Vote.belongsTo(app.model.Group, {foreignKey: 'group_id', targetKey: 'id', as: 'group'});
    app.model.Vote.belongsTo(app.model.User, {foreignKey: 'user_id', targetKey: 'id', as: 'user'});
  }
  return Vote
};
